package com.example.chatchatbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.chatchatbackend.entity.dto.Adv;
import com.example.chatchatbackend.entity.dto.Materials;
import com.example.chatchatbackend.entity.dto.Materials;

import com.example.chatchatbackend.entity.dto.Materials;
import com.example.chatchatbackend.entity.vo.request.AdvVO;
import com.example.chatchatbackend.entity.vo.response.MaterialsVO;
import com.example.chatchatbackend.entity.vo.response.MaterialsVO;
import com.example.chatchatbackend.mapper.MaterialsMapper;

import com.example.chatchatbackend.service.MaterialsService;
import com.example.chatchatbackend.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Service
public class MaterialsServiceImpl extends ServiceImpl<MaterialsMapper, Materials> implements MaterialsService {

    private static final LocalDateTime currentTime  = TimeUtil.Time_Now.getCurrentTime();

    @Autowired
    private MaterialsMapper materialsMapper;
    @Override
    public List<MaterialsVO> getAllMaterials() {
        List<Materials> materialsList = this.query().list();
        List<MaterialsVO> materialsVOList = new ArrayList<>();
        for(Materials materials : materialsList){
            materialsVOList.add(new MaterialsVO(materials.getId(),materials.getContent(),materials.getCreatedTime(),materials.getUpdateTime(),materials.getType()));
        }
        return materialsVOList;
    }

    @Override
    public MaterialsVO insertMaterials(MaterialsVO materialsVO) {
        Materials comment = new Materials(null,materialsVO.getContent(),this.currentTime,this.currentTime,materialsVO.getType());
        if(this.save(comment))
            return materialsVO;
        return null;
    }

    @Override
    public List<MaterialsVO> insertMaterialsList(List<MaterialsVO> materialsVOList) {

        List<Materials> materialsList = new ArrayList<>();

        for (MaterialsVO materialsVO : materialsVOList) {
            materialsList.add(new Materials(null,materialsVO.getContent(),currentTime,this.currentTime,materialsVO.getType()));
        }
        materialsMapper.insertList(materialsList);
        return materialsVOList;
    }


    @Override
    public IPage<Materials> pageThree(Integer pageNum, Integer pageSize) {
        Page<Materials> page = new Page<>(pageNum, pageSize);
        IPage<Materials> materialsIPage = materialsMapper.selectPage(page, null);
        return materialsIPage;
    }

    @Override
    public MaterialsVO updateMaterials(MaterialsVO materialsVO) {
        Materials materials = new Materials(materialsVO.getId(),materialsVO.getContent(),materialsVO.getCreatedTime(),this.currentTime,materialsVO.getType());
        materialsMapper.updateMaterials(materials);
        return materialsVO;
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        materialsMapper.deleteBatchIds(ids);
    }


    @Override
    public List<MaterialsVO> findByIds(List<Integer> ids) {
        List<Materials> materialsList = materialsMapper.selectBatchIds(ids);
        List<MaterialsVO> materialsVOList = new ArrayList<>();

        for (Materials materials : materialsList) {
            MaterialsVO materialsVO = new MaterialsVO();
            BeanUtils.copyProperties(materials, materialsVO);
            materialsVOList.add(materialsVO);  // 添加到结果列表
        }

        return materialsVOList;
    }

    public List<MaterialsVO> findByTitle(String title) {

        LambdaQueryWrapper<Materials> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(StringUtils.hasText(title), Materials::getContent, title);
        List<Materials> materialsList = materialsMapper.selectList(wrapper);

        List<MaterialsVO> materialsVOList = new ArrayList<>();

        for (Materials materials : materialsList) {
            MaterialsVO materialsVO = new MaterialsVO();
            BeanUtils.copyProperties(materials, materialsVO);
            materialsVOList.add(materialsVO);  // 添加到结果列表
        }
        return materialsVOList;
    }

}
